﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLNS_Client
{
    public partial class UC_CanBo_TDQLNN : UserControl
    {
        string temp = "";
        string temp1 = "";
        string flag = "";
        ServiceReference1.Service_QLNSClient sv = new ServiceReference1.Service_QLNSClient();
        public UC_CanBo_TDQLNN()
        {
            InitializeComponent();
        }

        private void btn_ThemTDCT_Click(object sender, EventArgs e)
        {
            frm_TDQLNN themtdqlnn = new frm_TDQLNN();
            themtdqlnn.ShowDialog();
        }

        private void UC_CanBo_TDQLNN_Load(object sender, EventArgs e)
        {
            Load_CB_TDQLNN();

            //Hiển thị tùy chọn trình độ quản lý nhà nước
            cmb_MaTDQLNN.DataSource = sv.getTDQLNN();
            cmb_MaTDQLNN.DisplayMember = "TenTDQLNN";
            cmb_MaTDQLNN.ValueMember = "Ma_TDQLNN";

            cmb_MaCB_TDQLNN.DataSource = sv.getCB_TDQLNN();
            cmb_MaCB_TDQLNN.DisplayMember = "Ma_CB";
            cmb_MaCB_TDQLNN.ValueMember = "Ma_CB";
        }
        public void Load_CB_TDQLNN()
        {
            dataGridView_TDQLNN.DataSource = sv.getCB_TDQLNN();
            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
        }

        private void dataGridView_TDQLNN_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            cmb_MaCB_TDQLNN.Text = dataGridView_TDQLNN.CurrentRow.Cells[0].Value.ToString();
            cmb_MaTDQLNN.Text = dataGridView_TDQLNN.CurrentRow.Cells[1].Value.ToString();
            date_NamDT_QLNN.Text = dataGridView_TDQLNN.CurrentRow.Cells[2].Value.ToString();
            txt_TruongDT_QLNN.Text = dataGridView_TDQLNN.CurrentRow.Cells[3].Value.ToString();

            temp = dataGridView_TDQLNN.CurrentRow.Cells[0].Value.ToString();
            temp1 = dataGridView_TDQLNN.CurrentRow.Cells[1].Value.ToString();
            cmb_MaCB_TDQLNN.Enabled = false;
            cmb_MaTDQLNN.Enabled = false;
            date_NamDT_QLNN.Enabled = false;
            txt_TruongDT_QLNN.Enabled = false;
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            flag = "them";
            cmb_MaCB_TDQLNN.Enabled = true;
            cmb_MaTDQLNN.Enabled = true;
            date_NamDT_QLNN.Enabled = true;
            txt_TruongDT_QLNN.Enabled = true;
            cmb_MaCB_TDQLNN.Text = "";
            cmb_MaTDQLNN.Text = "";
            date_NamDT_QLNN.Text = "";
            txt_TruongDT_QLNN.Text = "";
            btn_Luu.Enabled = true;
            btn_Huy.Enabled = true;
            btb_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = true;
            btb_Xoa.Enabled = true;
            btn_Sua.Enabled = true;

            ServiceReference1.CANBO_TDQLNN cbtdqlnn = new ServiceReference1.CANBO_TDQLNN();
            string a = cmb_MaCB_TDQLNN.Text;
            string b = cmb_MaTDQLNN.SelectedValue.ToString();
            string c = date_NamDT_QLNN.Text;
            string d = txt_TruongDT_QLNN.Text;

            cbtdqlnn.Ma_CB = a;
            cbtdqlnn.Ma_TDQLNN = b;
            cbtdqlnn.NamDaoTao = Convert.ToDateTime(c);
            cbtdqlnn.TruongDaoTao = d;

            if (a == "" | b == "" | c == "" | d == "")
            {
                MessageBox.Show("Xin nhập đầy đủ dữ liệu!");
            }
            else
            {
                int kq = 0;
                if (flag == "them")
                {
                    if (sv.checkCB_TDQLNN(cbtdqlnn))
                        kq = -1;
                    else
                        sv.addCB_TDQLNN(cbtdqlnn);
                }
                else
                {
                    if (flag == "sua")
                    {
                        sv.setCB_TDQLNN(cbtdqlnn);
                        kq = 1;
                    }
                }
                if (kq != -1)
                {
                    MessageBox.Show("Hoàn tất!");
                    Load_CB_TDQLNN();
                    btb_Xoa.Enabled = true;
                    btn_Sua.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Lỗi!!!");
                }
            }
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
            btb_Xoa.Enabled = true;
            btn_Sua.Enabled = true;
        }

        private void btb_Xoa_Click(object sender, EventArgs e)
        {
            ServiceReference1.CANBO_TDQLNN cbtdqlnn = new ServiceReference1.CANBO_TDQLNN();
            cbtdqlnn.Ma_CB = temp;
            cbtdqlnn.Ma_TDQLNN = temp1;
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn xóa dòng này?", "warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                sv.deleteCB_TDQLNN(cbtdqlnn);
                Load_CB_TDQLNN();
                MessageBox.Show("Đã xóa!");
                cmb_MaCB_TDQLNN.Text = "";
                cmb_MaTDQLNN.Text = "";
                date_NamDT_QLNN.Text = "";
                txt_TruongDT_QLNN.Text = "";
            }

        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            flag = "sua";
            btn_Luu.Enabled = true;
            btn_Huy.Enabled = true;
            date_NamDT_QLNN.Enabled = true;
            txt_TruongDT_QLNN.Enabled = true;
        }
    }
}
